<?php
declare (strict_types = 1);

namespace app\api\model\v1;

use think\Exception;
use think\exception\ValidateException;
use think\Model;

/**
 * @mixin \think\Model
 */
class TemplateSample extends AppModel
{
    //

    /**
     * @param $arrData
     * @param $uid
     * @User: 刘海龙
     * @Date: 2021/11/10
     * @Time: 17:54
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 导入样本点
     */
    public static function saveSampleExcel($arrData,$uid){
        if (!empty($arrData)) {
            foreach ($arrData as $key => $value) {
                $template_type = TemplateType::where(['template_type_title'=>trim($value['a'])])->find();
                if(!is_null($template_type)){
                    if (isset($value['item'])) { //判断是否包含item字段
                        foreach ($value['item'] as $child_key => $child_value) {
                            foreach ($child_value as $k =>$v){
                                $sample_template = self::where(['title'=>trim($v['b'])])->find();
                                if (is_null($sample_template)){
                                    self::create([
                                        'title'=>$v['b'],
                                        'template_type_id'=>$template_type->id,
                                        'create_id'=>$uid
                                    ]);
                                }
                            }

                        }
                    }
                }
            }
            $result = 200;
        }else{
            $result = 500;
        }
        return $result;

    }
}
